﻿
using SqlSugar;
using System.Text.RegularExpressions;

namespace Esst.Dto.Response
{
    /// <summary>
    /// 响应数据封装
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResponseHolder<T> : IResponseHolder
    {
        public ResponseHolder()
        {
        }


        public ResponseHolder(T data)
        {
            State = ResponseState.OK;
            Message = "";
            Data = data;
        }

        public ResponseHolder(ResponseState state)
        {   
            State = state;
            Message = "";
            Data = default;
        }
        public ResponseHolder(ResponseState state,string message)
        {
            State = state;
            Message = message;
            Data = default;
        }
        /// <summary>
        /// BadRequest结果
        /// </summary>
        /// <param name="message"></param>
        public ResponseHolder(string message)
        {
            State = ResponseState.BadRequest;
            Message = message;
            Data = default;
        }

        /// <summary>
        /// 响应状态
        /// </summary>
        public ResponseState State { get; set; } = ResponseState.OK;

        /// <summary>
        /// 响应信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 响应数据
        /// </summary>
        public T Data { get; set; }


        public static implicit operator ResponseHolder<T>(T value)
        {
            return new ResponseHolder<T>(value);
        }

        public static implicit operator ResponseHolder<T>(ResponseState state)
        {
            return new ResponseHolder<T>(state);
        }

        public static implicit operator ResponseHolder<T>(string message)
        {
            return new ResponseHolder<T>(message);
        }



        public static implicit operator ResponseHolder<T>(Result message)
        {
            return new ResponseHolder<T>()
            {
                State = message.Success ? ResponseState.OK : ResponseState.Error,
                Message = message.Reason
            };
        }

        public static implicit operator ResponseHolder<T>(Result<T> message)
        {
            return new ResponseHolder<T>()
            {
                State = message.Success ? ResponseState.OK : ResponseState.Error,
                Message = message.Reason,
                Data = message.Value
            };
        }

    }
}